//
//  UIButton+XYAdd.m
//  PeiJue
//
//  Created by lxy on 2017/9/28.
//  Copyright © 2017年 lixiya. All rights reserved.
//

#import "UIButton+XYAdd.h"
#import "UIControl+XYAdd.h"

@implementation UIButton (XYAdd)

/**
 *  快捷创建一个UIButton
 */
+ (UIButton *)buttonWithTitle:(nullable NSString *)title titleColor:(nullable UIColor *)titleColor font:(nullable UIFont *)font imageName:(nullable NSString *)imageName backImageName:(nullable NSString *)backImageName target:(id)target action:(SEL)action {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    
    if (title) {
        [button setTitle:title forState:UIControlStateNormal];
    }
    if (titleColor) {
        [button setTitleColor:titleColor forState:UIControlStateNormal];
    }
    if (font) {
        button.titleLabel.font = font;
    }
    if (imageName) {
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    if (backImageName) {
        [button setBackgroundImage:[UIImage imageNamed:backImageName] forState:UIControlStateNormal];
    }
    
    if (action) {
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    
    return button;
}

+ (UIButton *)buttonWithTitle:(nullable NSString *)title titleColor:(nullable UIColor *)titleColor font:(nullable UIFont *)font target:(id)target action:(SEL)action {
    
    return [self buttonWithTitle:title titleColor:titleColor font:font imageName:nil backImageName:nil target:target action:action];
}


/**
 *  快捷创建一个UIButton
 *  block回调
 */
+ (UIButton *)buttonWithTitle:(nullable NSString *)title titleColor:(nullable UIColor *)titleColor font:(nullable UIFont *)font imageName:(nullable NSString *)imageName backImageName:(nullable NSString *)backImageName block:(void(^)(UIButton *button))block {
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    
    if (title) {
        [button setTitle:title forState:UIControlStateNormal];
    }
    if (titleColor) {
        [button setTitleColor:titleColor forState:UIControlStateNormal];
    }
    if (font) {
        button.titleLabel.font = font;
    }
    if (imageName) {
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    if (backImageName) {
        [button setBackgroundImage:[UIImage imageNamed:backImageName] forState:UIControlStateNormal];
    }
    
    if (block) {
        
        [button xy_addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
            UIButton *mButton = (UIButton *)sender;
            block(mButton);
        }];
        
    }
    
    return button;
    
}


+ (UIButton *)buttonWithTitle:(nullable NSString *)title titleColor:(nullable UIColor *)titleColor font:(nullable UIFont *)font block:(void(^)(UIButton *button))block {
    
    return [self buttonWithTitle:title titleColor:titleColor font:font imageName:nil backImageName:nil block:block];
    
}

@end
